package javase2.day04;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo4 {
	public static void main(String[] args) throws IOException {
		char[] chs = {0x0041,0x0042,30000};
		System.out.println(chs);
		
		String file;
		OutputStream out;
		System.out.println("utf-16be");
		String str = "ABCDEF中国";
		file = "utf16be.txt";
		byte[] bytes = str.getBytes("UTF-16be");
		out=new FileOutputStream(file);
		out.write(bytes);
		out.close();
		IOUtils.print(file);
		
		System.out.println("UTF-8 :");
		file="utf8.txt";
		bytes = str.getBytes("utf-8");
		out=new FileOutputStream(file);
		out.write(bytes);
		out.close();
		IOUtils.print(file);
		
		byte[] utf8 = read("utf8.txt");
		byte[] utf16 = read("utf16be.txt");
		str=new String(utf8,"UTF-8");
		System.out.println(str);
		str=new String(utf16,"UTF-16");
		System.out.println(str);
		str=new String(utf8,"gbk");
		System.out.println(str);
	}
	public static byte[] read(String file){
		try{
			InputStream in = new FileInputStream(file);
			//适合小文件读取
			byte[] buf=new byte[in.available()];
			in.read(buf);
			in.close();
			return buf;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
			// TODO: handle exception
		}
	}
}
